-------------------------------------------------------------------------------
--
-------------------------------------------------------------------------------
local smtp = require("socket.smtp")
local io = require("io")

local msgBodyFileName = "./msgBodyConfirmation.txt"

dofile("common.lua")

-------------------------------------------------------------------------------
-- Function of send mail of confirmation of unregistration
-------------------------------------------------------------------------------
function sentMessageOfConfimationUnregistration(userID, email, password, IPMP)
	local from = "<"..getParameter("SMTP_USER")..">"
	local rcpt = "<"..tostring(email)..">"

	local msgRandomIDStr = "This is the acknowledgement of your unregistration in the DistriNet network.\n\n"
	local msgRandomIDStr = msgRandomIDStr.."Your ID is :  "..tostring(IPMP).."."..userID.."\n\n"
	
	msgRandomIDStr = msgRandomIDStr.."Please click on the following link to confirm your unregistration:".."\n\n"
	local masterPeer = tostring(IPMP)
	local port = getParameter("WEB_PORT")
	msgRandomIDStr = msgRandomIDStr.."http://"..masterPeer..":"..port.."/confirmation.lp?IPMP="..tostring(IPMP).."&userID="..tostring(userID).."&password="..tostring(password).."&type=unregistration"
	
	local msgBodyFile = assert(io.open(msgBodyFileName,"r"))
	local msgBodyStr = msgBodyFile:read("*a")
	msgBodyFile:close()
	
	local msg = {
		headers = {
			from = "<"..getParameter("SMTP_USER")..">",
			to = "<"..tostring(email)..">",
			subject = "Unregistration of Distrinet",
		},
		body = 	msgRandomIDStr.."\n"..msgBodyStr
	}
	
	local packet = {}
	packet.from = from
	packet.rcpt = rcpt
	packet.source = smtp.message(msg)
	packet.server = getParameter("SMTP_SERVER_NAME")
	packet.port = getParameter("SMTP_SERVER_PORT")
	packet.user = getParameter("SMTP_USER")
	--packet.password = getParameter("SMTP_PASSWORD")
	
	-- Send Email via SMTP
	local ok_send, sendErrStr = smtp.send(packet)
	if ok_send then
		print("C'est parfait !!! Envoyer")
	end
	
end
